package com.eshop.gen.service.impl;

import java.lang.invoke.SerializedLambda;
import java.io.File;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eshop.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.springframework.stereotype.Service;
import com.eshop.gen.service.GenConfigService;
import com.eshop.gen.domain.GenConfig;
import com.eshop.gen.service.mapper.GenConfigMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
public class GenConfigServiceImpl extends BaseServiceImpl<GenConfigMapper, GenConfig> implements GenConfigService
{
    public GenConfig find(final String tableName) {
        final GenConfig genConfig = (GenConfig)this.getOne( Wrappers.<GenConfig>lambdaQuery().eq(GenConfig::getTableName, (Object)tableName));
        if (genConfig == null) {
            return new GenConfig(tableName);
        }
        return genConfig;
    }
    
    public GenConfig update(final String tableName, final GenConfig genConfig) {
        if (StringUtils.isBlank((CharSequence)genConfig.getApiPath())) {
            final String separator = File.separator;
            final String symbol = "\\";
            String[] paths;
            if (symbol.equals(separator)) {
                paths = genConfig.getPath().split("\\\\");
            }
            else {
                paths = genConfig.getPath().split(File.separator);
            }
            final StringBuilder api = new StringBuilder();
            for (final String path : paths) {
                api.append(path);
                api.append(separator);
                if ("src".equals(path)) {
                    api.append("api");
                    break;
                }
            }
            genConfig.setApiPath(api.toString());
        }
        this.saveOrUpdate(genConfig);
        return genConfig;
    }
}
